/* 文件上传的处理
 * FileHandleAction.js
 */

import {uploadAudio,audioList,audioCategoryList,
		uploadFile,fileList,fileCategoryList
		} from '../api/FileApi';
import {uploadImgs,imglist,imgCategory} from '../api/ImageApi';

export function uploadAudioAct(files,category_id=0,callback=null){
	return (dispatch)=>{
		uploadAudio(files,category_id)
		.then(res=>{
			if(res.code==200){
				dispatch({type:'ADD_MUSIC',data:res.data});
				if(callback) callback(res.data.file);
			}else{
				if(callback) callback(null);
			}
		})
		.catch(e=>{
			console.log('FileHandleAction upload audio error');
			console.log(e);
			if(callback) callback(null);
		});
	}
}

export function listMusic(category_id=null,id=null,limit=null,callback=null){
	return (dispatch)=>{
		audioList({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'LIST_MUSIC',datalist:result.data});
				if(callback) callback(result.data);
			}else{
				if(callback) callback(null);
			}

		})
		.catch(e=>{
			console.log('FileHandleAction listMusic error');
			console.log(e);
		});
	}
}

//分页点击下一页
export function musicNextPage(maxId,category_id=null,id=null,limit=null){
	return (dispatch)=>{
		audioList({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'MUSIC_LIST_NEXT_PAGE',datalist:result.data});
				dispatch({type:'UPLOAD_LIST_NEXT_PAGE',maxId:maxId});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction musicNextPage error');
			console.log(e);
		});
	}
}

//分页点击上一页
export function musicPrePage(category_id=null,id=null,limit=null){
	return (dispatch)=>{
		audioList({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'MUSIC_LIST_PRE_PAGE',datalist:result.data});
				dispatch({type:'UPLOAD_LIST_PRE_PAGE'});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction musicPrePage error');
			console.log(e);
		});
	}
}

//获取music分类信息
export function categoryMusic(){
	return (dispatch)=>{
		audioCategoryList()
		.then(result=>{
			if(result.code==200){
				dispatch({type:'MUSIC_CATEGORY_LIST',datalist:result.data});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction category music error');
			console.log(e);
		});
	}
}

export function clearPageIdArr(){
	return {type:'UPLOAD_LIST_PAGE_CLEAR'};
}


export function clearImagelist(){
	return {type:'LIST_IMAGE',datalist:[]};
}

/* 图片列表查询
 * 参数id 查询小于该id的数据
 * category_id 分类id
 * limit 表示查询多少条
 */
export function listImage(category_id=null,id=null,limit=null,callback=null){
	return (dispatch)=>{
		imglist({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'LIST_IMAGE',datalist:result.data});
				if(callback) callback(result.data);
			}else{
				if(callback) callback(null);
			}

		})
		.catch(e=>{
			console.log('ImageHandleAction listImage error');
			console.log(e);
		});
	}
}

//分页点击下一页
export function imgNextPage(maxId,category_id=null,id=null,limit=null){
	//return {type:'IMAGE_LIST_NEXT_PAGE',maxId:maxId};
	return (dispatch)=>{
		imglist({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'IMAGE_LIST_NEXT_PAGE',datalist:result.data});
				dispatch({type:'UPLOAD_LIST_NEXT_PAGE',maxId:maxId});
			}
		})
		.catch(e=>{
			console.log('ImageHandleAction nextPage error');
			console.log(e);
		});
	}
}

//分页点击上一页
export function imgPrePage(category_id=null,id=null,limit=null){
	//return {type:'IMAGE_LIST_PRE_PAGE'};
	return (dispatch)=>{
		imglist({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'IMAGE_LIST_PRE_PAGE',datalist:result.data});
				dispatch({type:'UPLOAD_LIST_PRE_PAGE'});
			}
		})
		.catch(e=>{
			console.log('ImageHandleAction prePage error');
			console.log(e);
		});
	}
}

export function uploadImage(files,category_id=0,callback=null){
	return (dispatch)=>{
		uploadImgs(files,category_id)
		.then(res=>{
			if(res.code==200){
				console.log('upload back');
				dispatch({type:'ADD_IMAGE',data:res.data});
				if(callback) callback(res.data.file);
			}else{
				if(callback) callback(null);
			}
		})
		.catch(e=>{
			console.log('ImageHandleAction upload image error');
			console.log(e);
			if(callback) callback(null);
		});
	}
}

//获取image分类信息
export function categoryImage(){
	return (dispatch)=>{
		imgCategory()
		.then(result=>{
			if(result.code==200){
				dispatch({type:'IMAGE_CATEGORY_LIST',datalist:result.data});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction category image error');
			console.log(e);
		});
	}
}


export function uploadFileAct(files,category_id=0,callback=null){
	return (dispatch)=>{
		uploadFile(files,category_id)
		.then(res=>{
			if(res.code==200){
				dispatch({type:'ADD_FILE',data:res.data});
				if(callback) callback(res.data.file);
			}else{
				if(callback) callback(null);
			}
		})
		.catch(e=>{
			console.log('FileHandleAction upload file error');
			console.log(e);
			if(callback) callback(null);
		});
	}
}

export function listFile(category_id=null,id=null,limit=null,callback=null){
	return (dispatch)=>{
		fileList({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'LIST_FILE',datalist:result.data});
				if(callback) callback(result.data);
			}else{
				if(callback) callback(null);
			}

		})
		.catch(e=>{
			console.log('FileHandleAction listFile error');
			console.log(e);
		});
	}
}

//分页点击下一页
export function fileNextPage(maxId,category_id=null,id=null,limit=null){
	return (dispatch)=>{
		fileList({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'FILE_LIST_NEXT_PAGE',datalist:result.data});
				dispatch({type:'UPLOAD_LIST_NEXT_PAGE',maxId:maxId});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction fileNextPage error');
			console.log(e);
		});
	}
}

//分页点击上一页
export function filePrePage(category_id=null,id=null,limit=null){
	return (dispatch)=>{
		fileList({id:id,category_id:category_id,limit:limit})
		.then(result=>{
			if(result.code==200){
				dispatch({type:'FILE_LIST_PRE_PAGE',datalist:result.data});
				dispatch({type:'UPLOAD_LIST_PRE_PAGE'});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction filePrePage error');
			console.log(e);
		});
	}
}

//获取music分类信息
export function categoryFile(){
	return (dispatch)=>{
		fileCategoryList()
		.then(result=>{
			if(result.code==200){
				dispatch({type:'FILE_CATEGORY_LIST',datalist:result.data});
			}
		})
		.catch(e=>{
			console.log('FileHandleAction category file error');
			console.log(e);
		});
	}
}